/**
 * The beta view.
 *
 * (c) Copyright 2011 Nokia Corporation. All rights reserved.
 */

import QtQuick 1.1
import com.nokia.symbian 1.1

import "../script/common.js" as CommonScript

Item {
    id: betaViewContainer;

    // Signalled when an item is selected from the list.
    signal itemClicked(int id);

    Text {
        id: noResultsInfo;
        width: parent.width;
        anchors.centerIn: parent;
        text: "No search results found matching your search criteria.";
        opacity: 0;
        wrapMode: "WordWrap";
    }

    ScrollDecorator {
        flickableItem: betaList;
    }

    ListView {
        id: betaList;
        anchors.fill: parent;
        model: betaModel;
        delegate: betaItem;
        spacing: 0;
        snapMode: ListView.SnapToItem;
        // The TopBar component is used as a header, so it will scroll nicely
        // along with the list content.
        header: TopBar {
            width: parent.width;
            height: 70;
            // Bind the top bar's title image to the custom "headerImage"
            // property to offer a way to change the list's title.
            titleImage: betaList.headerImage;
        }

        // The image to be shown in the list's header.
        property string headerImage;
    }

    XmlListModel {
        id: betaModel;
        query: "/betalist/beta";
        onStatusChanged: {
            if (status == XmlListModel.Loading) {
                busyIndicator.running = true;
                busyIndicator.opacity = 1;
            } else {
                busyIndicator.opacity = 0;
                busyIndicator.running = false;
                // If there are no search results, tell it to the user.
                if (betaList.count == 0) {
                    noResultsInfo.opacity = 1;
                } else {
                    noResultsInfo.opacity = 0;
                }
            }
        }

        XmlRole {
            name: "id";
            query: "id/string()";
        }

        XmlRole {
            name: "title";
            query: "title/string()";
        }

        XmlRole {
            name: "thumb";
            query: "thumb/string()";
        }
    }

    Component {
        id: betaItem;

        ListItem {
            height: Math.max(betaImage.height + 20, 70);
            subItemIndicator: true;
            platformInverted: true;

            Row {
                width: parent.width;
                height: parent.height;
                anchors.top: parent.top;
                anchors.topMargin: 10;
                spacing: 10;

                Image {
                    id: betaImage;
                    source: thumb;
                }
                Text {
                    width: 4 * (parent.width / 5);
                    text: title;
                    wrapMode: "WordWrap";
                }
            }

            Rectangle {
                width: parent.width;
                height: 1;
                border.color: "black";
            }

            MouseArea {
                anchors.fill: parent;
                onClicked: {
                    betaList.currentIndex = index;
                    betaItemClicked(id);
                }
            }
        }
    }

    // Gets the latest betas.
    function getLatest() {
        betaList.headerImage = "qrc:/gfx/title_latest_applications.png";
        var url = CommonScript.BETA_LABS_API_URL + "/search"
                + "?state=" + CommonScript.DEFAULT_STATE
                + "&maturity=" + CommonScript.DEFAULT_MATURITY
                + "&order=updated";
        betaModel.source = url;
    }

    // Gets the most popular betas.
    function getMostPopular() {
        betaList.headerImage = "qrc:/gfx/title_most_popular_applications.png";
        var url = CommonScript.BETA_LABS_API_URL + "/search"
                + "?state=" + CommonScript.DEFAULT_STATE
                + "&maturity=" + CommonScript.DEFAULT_MATURITY
                + "&order=rating";
        betaModel.source = url;
    }

    // Gets betas based on the specified tags.
    function getBasedOnTags(tags) {
        betaList.headerImage = "qrc:/gfx/title_search.png";
        var url = CommonScript.BETA_LABS_API_URL + "/search"
                + "?state=" + CommonScript.DEFAULT_STATE
                + "&maturity=" + CommonScript.DEFAULT_MATURITY
                + "&tags=" + tags
                + "&order=title";
        betaModel.source = url;
    }

    // Gets betas based on the specified free text phrase.
    function getBasedOnFreeText(freeTextPhrase) {
        betaList.headerImage = "qrc:/gfx/title_search.png";
        // Encode the phrase string into standard HTML URI format
        freeTextPhrase = escape(freeTextPhrase);

        var url = CommonScript.BETA_LABS_API_URL + "/search-text/"
            + freeTextPhrase;
        betaModel.source = url;
    }

    // Called when an item is selected from the list.
    function betaItemClicked(id) {
        busyIndicator.running = true;
        busyIndicator.opacity = 1;

        betaViewContainer.itemClicked(id);
    }
}
